package com.jdk.learn.thread;

/**
 * synchronized 默认锁的是当前对象，而当前对象这是可以重入的
 */
public class LockSynchronizeDemo2 implements Runnable{
    @Override
    public void run() {
        get();
        set();
    }

    public synchronized void get(){
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("name:"+Thread.currentThread().getName()+",get");
//        set();
    }
    public synchronized void set(){
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("name:"+Thread.currentThread().getName()+",set");
    }

    public static void main(String[] args) {
        LockSynchronizeDemo2 lockSynchronizeDemo=new LockSynchronizeDemo2();
        new Thread(lockSynchronizeDemo,"1").start();
        new Thread(lockSynchronizeDemo,"2").start();
        new Thread(lockSynchronizeDemo,"3").start();
        new Thread(lockSynchronizeDemo,"4").start();
    }
}
